﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.数据查询
{
    /// <summary>
    /// 数据查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class DataQueryController : BaseController
    {
        private readonly IInterfaceLogRepository _interfaceLogRepo;
        private readonly IInstructService instructService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="instructService"></param>
        public DataQueryController(IWebHostEnvironment environment, IInstructService instructService, IInterfaceLogRepository interfaceLogRepo) : base(environment)
        {
            this.instructService = instructService;
            _interfaceLogRepo = interfaceLogRepo;
        }


        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="logid"></param>
        /// <param name="staid"></param>
        /// <param name="jname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
       
        public IActionResult GetListLog(int? logid, int? staid, string? jname, int pageIndex = 1, int pageSize = 50)
        {
            // 获取所有接口日志
            var query = _interfaceLogRepo.GetEntityAlls();

            // 添加查询条件
            if (logid.HasValue)
            {
                query = query.Where(x => x.LogType == logid);
            }

            if (staid.HasValue)
            {
                query = query.Where(x => x.Status == staid);
            }

            if (!string.IsNullOrEmpty(jname))
            {
                query = query.Where(c => c.Name.Contains(jname));
            }

            // 获取总记录数
            int totalCount = query.Count();

            // 分页处理并排序
            var list = query.OrderByDescending(o => o.Id)
                            .Skip((pageIndex - 1) * pageSize)
                            .Take(pageSize)
                            .ToList();

            // 返回查询结果
            if (list.Any())
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            else
            {
                return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
            }
        }


        /// <summary>
        /// 获取指令列表
        /// </summary>
        /// <param name="InstructType"></param>
        /// <param name="Status"></param>
        /// <param name="PalletBarcode"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInstructList(int InstructType = 0, int Status = 0, string? PalletBarcode = "", int pageIndex = 1, int pageSize = 8)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = instructService.GetInstructList(InstructType, Status, PalletBarcode, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 指令-货品明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetInstructMater(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = instructService.GetInstructMater(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 撤销指令
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult CancelInstruct(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string message = "";
            bool result = instructService.CancelInstruct(id, out message);
            if (result)
                return Ok(BaseResult.Result(true, "撤销成功！", "200"));
            return Ok(BaseResult.Result(false, "删除失败！" + message, "500"));
        }
    }
}
